package cn.cc.baycar.service.serviceImpl;

import cn.cc.baycar.mapper.mapper.SmallMiGoodsPayCarMapper;
import cn.cc.baycar.mapper.pojo.SmallMiGoodsPayCar;
import cn.cc.baycar.service.PayCarService;
import cn.cc.baycar.service.bo.AddPayCarDO;
import cn.cc.common.beanCopy.utils.BeanCopierUtil;
import cn.cc.common.contants.RedisKeyConstant;
import cn.cc.common.redis.RedisCommon;
import cn.cc.common.result.utils.RestResult;
import cn.cc.common.result.utils.RestResultEmum;
import cn.cc.goods.service.GoodsService;
import cn.cc.goods.service.domain.EsResultSourceDO;
import cn.cc.goods.service.domain.GetGoodsSkuAndSpuDO;
import cn.cc.goods.service.domain.GetGoodsSkuDO;
import cn.hutool.core.lang.generator.ObjectIdGenerator;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;

/**
 * @program: smail-mi-cloud
 * @description: 购物车功能实现
 * @author: Mr.WuChao
 * @create: 2021-07-21 14:49
 **/
@Service
public class PayCarServiceImpl implements PayCarService {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private RedisCommon redisCommon;

    @Override
    public RestResult<Void> addPayCarByCheck(AddPayCarDO addPayCarDO) {
        //通过goodsid查询出用户所选的商品
        GetGoodsSkuAndSpuDO goodsData = goodsService.getGoodsSkuAndSpu(addPayCarDO.getGoods_id()).getData();
        //判断商品是否上架  是否有库存  是否有货
        if (null == goodsData.getGoodsRepertory() || "" == goodsData.getGoodsRepertory()) {
            return new RestResult<>(RestResultEmum.EXIT_ERROR.code, RestResultEmum.EXIT_ERROR.msg);
        }
        if (null == goodsData.getGoodsStatus() || "" == goodsData.getGoodsStatus()) {
            return new RestResult<>(RestResultEmum.EXIT_ERROR.code, RestResultEmum.EXIT_ERROR.msg);
        }
        if (null == goodsData.getGoodsStatusStop() || "" == goodsData.getGoodsStatusStop()) {
            return new RestResult<>(RestResultEmum.EXIT_ERROR.code, RestResultEmum.EXIT_ERROR.msg);
        }
        //skuid
        addPayCarDO.setSku_id(goodsData.getSkuId());
        //自动生成id
        ObjectIdGenerator objectIdGenerator = new ObjectIdGenerator();
        String id = objectIdGenerator.next();
        //购物车id
        addPayCarDO.setPay_car_id(id);
        //商品id
        addPayCarDO.setGoods_id(goodsData.getGoodsId());
        //商品名称
        addPayCarDO.setGoods_name(goodsData.getGoodsName());
        //商品单价
        addPayCarDO.setGoods_price(goodsData.getGoodsPrice());
        //创建时间
        addPayCarDO.setCreation_date(new Date());
        //创建者
        addPayCarDO.setCreated_by("cc");
        //修改者
        addPayCarDO.setModify_date(new Date());
        //计算价格
        BigDecimal goodsPrice = addPayCarDO.getGoods_price();
        String goodsNum = addPayCarDO.getGoods_num();
        int price = Integer.parseInt(goodsPrice.toString());
        int num = Integer.parseInt(goodsNum);
        int sumPrice = price * num;
        addPayCarDO.setTotal_price(new BigDecimal(sumPrice));
        //小米id
        //TODO 调用用户接口插入小米id
        addPayCarDO.setMi_id("0901");
        //取出redis数据
        Map<String, AddPayCarDO> hashAll = redisCommon.getHashAll(RedisKeyConstant.SHOPPING_CAR_GOODS_CC_PAY_CAR, AddPayCarDO.class);
        if (null != hashAll || hashAll.size() != 0) {
            //如果拿到数据 通过key值拿到具体数据
            AddPayCarDO carDO = hashAll.get(addPayCarDO.getSku_id());
            //如果拿到了肯定数据有变化直接更新redis
            if (null != carDO) {
                redisCommon.putHash(RedisKeyConstant.SHOPPING_CAR_GOODS_CC_PAY_CAR, addPayCarDO.getSku_id(), addPayCarDO);
            }
        }
        //如果没有就把数据存入redis
        redisCommon.putHash(RedisKeyConstant.SHOPPING_CAR_GOODS_CC_PAY_CAR, addPayCarDO.getSku_id(), addPayCarDO);
        String str = "redis监听";
        redisCommon.setObj(RedisKeyConstant.SHOPPING_CAR_GOODS_CC_PAY_CAR_LISTENER, str, 30L);
        return new RestResult<>();
    }


    @Override
    public RestResult<Void> addPayCarByEs(AddPayCarDO addPayCarDO) throws IOException {
        //模糊查询到的数据
        Map<String, EsResultSourceDO> goodsSpu = goodsService.esGetSpuName("goods_spu", addPayCarDO.getGoods_name()).getData();
        //随机拿出一条数据相当于用户选择的
        List<EsResultSourceDO> list=new ArrayList<>();
        Iterator<EsResultSourceDO> iterator = goodsSpu.values().iterator();
        while (iterator.hasNext()) {
            EsResultSourceDO next = iterator.next();
            list.add(next);
        }
        int i = (int) (Math.random() * list.size());
        EsResultSourceDO sourceDO = list.get(i);
        //通过goodsid查询出用户所选的商品
        GetGoodsSkuAndSpuDO goodsData = goodsService.getGoodsSkuAndSpu(sourceDO.getGoods_id()).getData();
        //判断商品是否上架  是否有库存  是否有货
        if (null == goodsData.getGoodsRepertory() || "" == goodsData.getGoodsRepertory()) {
            return new RestResult<>(RestResultEmum.EXIT_ERROR.code, RestResultEmum.EXIT_ERROR.msg);
        }
        if (null == goodsData.getGoodsStatus() || "" == goodsData.getGoodsStatus()) {
            return new RestResult<>(RestResultEmum.EXIT_ERROR.code, RestResultEmum.EXIT_ERROR.msg);
        }
        if (null == goodsData.getGoodsStatusStop() || "" == goodsData.getGoodsStatusStop()) {
            return new RestResult<>(RestResultEmum.EXIT_ERROR.code, RestResultEmum.EXIT_ERROR.msg);
        }
        //skuid
        addPayCarDO.setSku_id(goodsData.getSkuId());
        //自动生成id
        ObjectIdGenerator objectIdGenerator = new ObjectIdGenerator();
        String id = objectIdGenerator.next();
        //购物车id
        addPayCarDO.setPay_car_id(id);
        //商品id
        addPayCarDO.setGoods_id(goodsData.getGoodsId());
        //商品名称
        addPayCarDO.setGoods_name(goodsData.getGoodsName());
        //商品单价
        addPayCarDO.setGoods_price(goodsData.getGoodsPrice());
        //创建时间
        addPayCarDO.setCreation_date(new Date());
        //创建者
        addPayCarDO.setCreated_by("cc");
        //修改者
        addPayCarDO.setModify_date(new Date());
        //计算价格
        BigDecimal goodsPrice = addPayCarDO.getGoods_price();
        String goodsNum = addPayCarDO.getGoods_num();
        int price = Integer.parseInt(goodsPrice.toString());
        int num = Integer.parseInt(goodsNum);
        int sumPrice = price * num;
        addPayCarDO.setTotal_price(new BigDecimal(sumPrice));
        //小米id
        //TODO 调用用户接口插入小米id
        addPayCarDO.setMi_id("0901");
        //取出redis数据
        Map<String, AddPayCarDO> hashAll = redisCommon.getHashAll(RedisKeyConstant.SHOPPING_CAR_GOODS_CC_PAY_CAR, AddPayCarDO.class);
        if (null != hashAll || hashAll.size() != 0) {
            //如果拿到数据 通过key值拿到具体数据
            AddPayCarDO carDO = hashAll.get(addPayCarDO.getSku_id());
            //如果拿到了肯定数据有变化直接更新redis
            if (null != carDO) {
                redisCommon.putHash(RedisKeyConstant.SHOPPING_CAR_GOODS_CC_PAY_CAR, addPayCarDO.getSku_id(), addPayCarDO);
            }
        }
        //如果没有就把数据存入redis
        redisCommon.putHash(RedisKeyConstant.SHOPPING_CAR_GOODS_CC_PAY_CAR, addPayCarDO.getSku_id(), addPayCarDO);
        String str = "redis监听";
        redisCommon.setObj(RedisKeyConstant.SHOPPING_CAR_GOODS_CC_PAY_CAR_LISTENER, str, 30L);
        return new RestResult<>();
    }


    @Override
    public RestResult<Void> updPayCarByCheck(AddPayCarDO addPayCarDO) {
        //通过spuid 从redis查询要修改的数据
        Map<String, AddPayCarDO> hashAll = redisCommon.getHashAll(RedisKeyConstant.SHOPPING_CAR_GOODS_CC_PAY_CAR, AddPayCarDO.class);
        AddPayCarDO goodsData = hashAll.get(addPayCarDO.getSku_id());
        //TODO 判断库存是否有
        //修改时间
        addPayCarDO.setModify_date(new Date());
        //修改者
        addPayCarDO.setModifie_by("cc");
        //计算价格
        BigDecimal goodsPrice = goodsData.getGoods_price();
        String goodsNum = addPayCarDO.getGoods_num();
        goodsData.setGoods_num(goodsNum);
        int price = Integer.parseInt(goodsPrice.toString());
        int num = Integer.parseInt(goodsNum);
        int sumPrice = price * num;
        goodsData.setTotal_price(new BigDecimal(sumPrice));
        //如果没有就把数据存入redis
        redisCommon.putHash(RedisKeyConstant.SHOPPING_CAR_GOODS_CC_PAY_CAR, addPayCarDO.getSku_id(), goodsData);
        String str = "redis监听";
        redisCommon.setObj(RedisKeyConstant.SHOPPING_CAR_GOODS_CC_PAY_CAR_LISTENER, str, 30L);
        return new RestResult<>();
    }


    @Override
    public RestResult<List<AddPayCarDO>> getPayCar() {
        Map<String, AddPayCarDO> hashAll = redisCommon.getHashAll(RedisKeyConstant.SHOPPING_CAR_GOODS_CC_PAY_CAR, AddPayCarDO.class);
        Iterator<AddPayCarDO> iterator = hashAll.values().iterator();
        List<AddPayCarDO> list=new ArrayList<>();
        while (iterator.hasNext()){
            AddPayCarDO next = iterator.next();
            list.add(next);
        }
        return new RestResult<>(list);
    }
}
